home *** CD-ROM | disk | FTP | other *** search
/ Animation / Animation Vol.1 (Profi ROM)(1994).iso / source.zip / RAYOPT.C < prev    next >
C/C++ Source or Header  |  1993-10-01  |  60KB  |  2,302 lines

  1. /*-------------------------------------------------------------------------
  2.  
  3.          Triangle Bounder/Smoother for POV-Ray
  4.             Copyright (c) 1993 Steve Anger
  5.  
  6.     A number of C routines that can be used to generate POV-Ray ray tracer
  7.  files from triangle data.  Supports generation of smooth triangles and an
  8.  optimal set of bounding shapes for much faster traces.  Output files are
  9.  compatible with POV-Ray v1.0.  This program may be freely modified and
  10.  distributed.
  11.                        Compuserve: 70714,3113
  12.                         YCCMR BBS: (708)358-5611
  13.  
  14. --------------------------------------------------------------------------*/
  15.  
  16. #ifdef __TURBOC__
  17. #include <alloc.h>
  18. #endif
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <math.h>
  25. #include "vect.h"
  26. #include "rayopt.h"
  27.  
  28. #if defined(applec) || defined(THINK_C)
  29. #include "RAW2POV.mac.h"
  30. #else
  31. #define COOPERATE
  32. #endif
  33.  
  34. #define HASHSIZE  1000          /* Size of hash table for vertex lookup */
  35. #define DEGEN_TOL (1e-8)        /* float comparison tolerance for checking */
  36.                 /* for degenerate triangles */
  37. #define MAX_TEX   500           /* Maximum allowable number of texture */
  38.                 /* declarations */
  39.  
  40. #define POV10   0
  41. #define POV20   1
  42. #define VIVID   2
  43. #define POLYRAY 3
  44.  
  45. #ifndef MAXFLOAT
  46. #define MAXFLOAT (1e37)
  47. #endif
  48.  
  49. typedef struct {
  50.     float red;
  51.     float green;
  52.     float blue;
  53. } Palette;
  54.  
  55. typedef char *Texture;
  56.  
  57. typedef struct {
  58.     unsigned vert[3];
  59.     unsigned text_index;
  60.     char     text_type;
  61.     char     flag;
  62. } Triangle;
  63.  
  64.  
  65. /* Linked list of triangles */
  66. typedef struct TList {
  67.     Triangle     *tri;
  68.     struct TList *next;
  69. } TriList;
  70.  
  71.  
  72. /* Double linked list of triangles */
  73. typedef struct TList2 {
  74.     Triangle      *tri;
  75.     struct TList2 *prev;
  76.     struct TList2 *next;
  77. } TriList2;
  78.  
  79.  
  80. /* List of triangle vertices */
  81. typedef struct VList {
  82.     unsigned     vert;
  83.     struct VList *next;
  84. } VertList;
  85.  
  86.  
  87. /* List of triangle groups */
  88. typedef struct GTree {
  89.     TriList2     *index[3];    /* Triangles indexed by x, y, and z coord */
  90.     Vector       vmin;         /* min/max extents of triangle vertices */
  91.     Vector       vmax;         /*    "       "     "     "        "     */
  92.     float        area;         /* Total surface area of bounding region */
  93.     unsigned     obj_cnt;      /* Total number of triangles in group */
  94.     int          child_cnt;    /* Number of children */
  95.     int          split_cnt;    /* Number of times this node has been split */
  96.     struct GTree *parent;      /* Parent of this node */
  97.     struct GTree *next;        /* Next node at this level */
  98.     struct GTree *child;       /* First child of this ndoe */
  99. } GroupTree;
  100.  
  101. static Palette   *ptable;      /* Palette table */
  102. static unsigned  pmax;         /* Maximum size of table */
  103. static unsigned  psize;        /* Current size */
  104.  
  105. static Texture   *ttable;      /* Named texture table */
  106. static unsigned  tmax;         /* Maximum size of table */
  107. static unsigned  tsize;        /* Current size */
  108.  
  109. static Vector    *vtable;      /* Vertice table */
  110. static unsigned  vmax;         /* Maximum size of table */
  111. static unsigned  vsize;        /* Current size */
  112.  
  113. static Vector    gmin = {+MAXFLOAT, +MAXFLOAT, +MAXFLOAT};
  114. static Vector    gmax = {-MAXFLOAT, -MAXFLOAT, -MAXFLOAT};
  115.  
  116. static Matrix    trans_matrix;
  117. static int       use_transform = 0;
  118.  
  119. static VertList  **vert_hash;    /* Hash table for looking up vertices */
  120. static TriList   **tri_index;    /* Index for smooth triangle generation */
  121.  
  122. static GroupTree *groot;         /* Tree representing the object hierarchy */
  123.  
  124. static int       initialized  = 0;
  125. static int       quiet_mode   = 0;
  126. static int       bound_mode   = 0;
  127. static float     smooth_angle = 0.0;
  128. static unsigned  vert_init    = 0;
  129. static int       dec_point    = 4;
  130. static int       out_format   = POV10;
  131.  
  132. static char      out_file[64] = "rayopt.pov";
  133. static char      inc_file[64] = "rayopt.inc";
  134.  
  135. static unsigned  tot_bounds   = 0;
  136. static unsigned  object_cnt   = 0;
  137.  
  138. static Vector    last_vmin = {0.0, 0.0, 0.0};
  139. static Vector    last_vmax = {0.0, 0.0, 0.0};
  140. static unsigned  last_vert_cnt = 0;
  141. static unsigned  last_tri_cnt = 0;
  142. static float     last_index = 0.0;
  143. static unsigned  last_bounds = 0;
  144.  
  145. static Palette   last_pal;
  146. static char      last_texture[64] = "";
  147. static unsigned  texture_index;
  148. static char      texture_type;
  149. static char      object_name[64] = "";
  150.  
  151. static float     orig_tpr;    /* Number of Tests Per Ray before optimization */
  152. static float     final_tpr;   /*    "   "    "    "   "  after optimization */
  153. static float     bound_cost;  /* Cost of testing a bound relative to testing */
  154.                   /* a triangle */
  155.  
  156. /* Function prototypes */
  157. void init_object (void);
  158. void cleanup_object (void);
  159. float calc_tpr (GroupTree *gnode);
  160. GroupTree *create_group (void);
  161. void delete_tree (GroupTree *gnode);
  162. void optimize_tree (GroupTree *gnode);
  163. void test_split (GroupTree *gnode, int axis, float *best_rtpr, TriList2 **
  164.     best_loc);
  165. void split_group (GroupTree *gnode, int axis, TriList2 *split_loc, GroupTree *
  166.     *group_a, GroupTree **group_b);
  167. void write_file (void);
  168. void write_box (Vector v1, Vector v2, Triangle *tri);
  169. void write_pov10_tree (FILE *f, GroupTree *gnode, int level);
  170. void write_pov10_texture (FILE *f, Triangle *tri);
  171. void write_pov10_transform (FILE *f, Matrix matrix);
  172. void write_pov10_header (FILE *f);
  173. void write_pov10_triangle (FILE *f, Triangle *tri, int one_texture);
  174. void write_pov10_bound (FILE *f, GroupTree *gnode);
  175. void write_pov20_tree (FILE *f, GroupTree *gnode, int level);
  176. void write_pov20_texture (FILE *f, Triangle *tri);
  177. void write_pov20_transform (FILE *f, Matrix matrix);
  178. void write_pov20_header (FILE *f);
  179. void write_pov20_triangle (FILE *f, Triangle *tri, int one_texture);
  180. void write_pov20_bound (FILE *f, GroupTree *gnode);
  181. void write_vivid_tree (FILE *f, GroupTree *gnode);
  182. void write_vivid_transform (FILE *f, Matrix matrix);
  183. void write_vivid_texture (FILE *f, Triangle *tri);
  184. void write_vivid_header (FILE *f);
  185. void write_vivid_triangle (FILE *f, Triangle *tri);
  186. void write_polyray_tree (FILE *f, GroupTree *gnode);
  187. void write_polyray_transform (FILE *f, Matrix matrix);
  188. void write_polyray_texture (FILE *f, Triangle *tri);
  189. void write_polyray_header (FILE *f);
  190. void write_polyray_triangle (FILE *f, Triangle *tri);
  191. void update_node (GroupTree *gnode);
  192. void sort_indexes (GroupTree *gnode);
  193. void quick_sort (TriList2 *start, TriList2 *end, int axis);
  194. float surf_area (float a, float b, float c);
  195. float max_vertex (Triangle *tri, int axis);
  196. float min_vertex (Triangle *tri, int axis);
  197. float avg_vertex (Triangle *tri, int axis);
  198. void build_tri_index (void);
  199. void dump_tri_index (void);
  200. void vert_normal (Triangle *t, Vector *norm);
  201. void tri_normal (Triangle *t, Vector normal);
  202. unsigned pal_lookup (float red, float green, float blue);
  203. unsigned texture_lookup (char *texture_name);
  204. unsigned vert_lookup (float x, float y, float z);
  205. int degen_tri (float ax, float ay, float az, float bx, float by, float bz,
  206.      float cx, float cy, float cz);
  207. void abortmsg (char *msg, int exit_code);
  208. float fmin (float a, float b);
  209. float fmax (float a, float b);
  210. void add_ext (char *fname, char *ext, int force);
  211. void cleanup_name (char *name);
  212.  
  213.  
  214. void opt_set_format (int format)
  215. {
  216.     if (format != POV10 && format != POV20 && format != VIVID && format != POLYRAY)
  217.     abortmsg ("ERROR: Invalid parameter passed to opt_set_format.", 1);
  218.  
  219.     out_format = format;
  220. }
  221.  
  222.  
  223. void opt_set_fname (char *out_name, char *inc_name)
  224. {
  225.     FILE *f;
  226.  
  227.     strcpy (out_file, out_name);
  228.  
  229.     if (strlen(inc_name) > 0)
  230.     strcpy (inc_file, inc_name);
  231.     else {
  232.     strcpy (inc_file, out_file);
  233.  
  234.     switch (out_format) {
  235.         case POV10:
  236.         case POV20:   add_ext (inc_file, "inc", 1);
  237.               break;
  238.         case VIVID:   add_ext (inc_file, "vo", 1);
  239.               break;
  240.         case POLYRAY: add_ext (inc_file, "inc", 1);
  241.               break;
  242.     }
  243.     }
  244.  
  245.     if (strcmp (out_file, inc_file) == 0)
  246.     abortmsg ("Main file and include file cannot have the same na